package cn.javaxc.model.page;
import java.net.*;
import org.apache.ibatis.session.*;
import org.apache.ibatis.mapping.*;

import cn.javaxc.model.base.*;
import cn.javaxc.model.page.VUEPage;
import cn.javaxc.mvcwork.util.helper.*;
import cn.javaxc.mvcwork.controller.web.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.javaxc.mvcwork.framework.actions.TActionContext;
import org.mybatis.spring.SqlSessionTemplate;
public class BatisPage  extends VUEPage{
//	public BatisPage(){
//		for (Class clazz=this.getClass();clazz!=BatisPage.class;clazz=clazz.getSuperclass()){
//			new BatisRefresh().refresh(clazz,null);
//		}
//	}
//	public void refresh(Class clazz,String url) {
//		new BatisRefresh().refresh(clazz,null);
//	}
  @Autowired
  protected SqlSessionFactory factory;
//  public BatisPage(){
//	  factory=(SqlSessionFactory)ObjectHelper.getBean("sqlSessionFactory");
//  }
//  public void setContext(TActionContext context){
//	  super.setContext(context);
//	  buildMapper();
//  }
//	public void createPage(VUEPage _page){
//		super.createPage(_page);
//		if (_page instanceof BatisPage){
//			if (factory==null){
//				factory=((BatisPage)_page).factory;			
//			}
//		}
//	}
//    public BatisList createList(){
//		return new BatisList();
//    }
//  public BatisElement createElement(){
//	return new BatisElement();
//  }		
  public SqlSession openSession(){
	return null;
  }
  public TreeBean createTreeBean(String filter){
	 return createTreeBean(this.getClass(),filter);
  }
	public TreeBean createTreeBean(Class clazz,String filter){
		TreeBean tb=new TreeBean();
//		SqlSessionFactory factory=(SqlSessionFactory)ObjectHelper.getBean("sqlSessionFactory");
		String clazzName=clazz.getName();
		if (clazzName.endsWith("Action")){
			clazzName=clazzName.substring(0,clazzName.length()-"Action".length());
		}
		ResultMap result=factory.getConfiguration().getResultMap(clazzName.replaceFirst("\\.action\\.",".mapper.")+"Mapper."+filter);
		for (ResultMapping map:result.getResultMappings() ){
			if ("treeNodeId".equals(map.getProperty())){
				tb.setTreeNodeId(map.getColumn());
			}else if ("treeNodeName".equals(map.getProperty())){
				tb.setTreeNodeName(map.getColumn());
			}else if ("treeNodePid".equals(map.getProperty())){
				tb.setTreeNodePid(map.getColumn());
			}
		}
		return tb;
	}
	public SQLList buildExcelTitle(String filter){
	 return buildExcelTitle(this.getClass(),filter);		
	}

	public SQLList buildExcelTitle(Class clazz,String filter){
		SQLList list=new SQLList();
		SQLMap emt=new SQLMap();
		list.add(emt);
//		SqlSessionFactory factory=(SqlSessionFactory)ObjectHelper.getBean("sqlSessionFactory");
		String clazzName=clazz.getName();
		if (clazzName.endsWith("Action")){
			clazzName=clazzName.substring(0,clazzName.length()-"Action".length());
		}
		ResultMap result=factory.getConfiguration().getResultMap(clazzName.replaceFirst("\\.action\\.",".mapper.")+"Mapper."+filter);
		for (ResultMapping map:result.getResultMappings() ){
			list.getColumnNames().add(map.getColumn());
			emt.put(map.getColumn(),map.getProperty());
		}
		return list;
	}
	public String getSql(String id){
		return getClass().getName()+"."+id;
	}
	public String getSql(Class clazz,String id){
		return clazz.getName()+"."+id;
	}
	public String getPageSql(String id){
		return getClass().getName()+"."+id+"_count";
	}
	public String getPageSql(Class clazz,String id){
		return clazz.getName()+"."+id+"_count";
	}
//	public <T> T getMapper(Class<T> clazz){
//		if (factory!=null){
//			return new SqlSessionTemplate(factory).getMapper(clazz);
//		}
//		throw new RuntimeException("NULL");
//	}
}
